import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;

/**
 * Authors - Oren Bracha, Rony Lahav & Yehuda Eterman
 * This class represents one tuple in the DB (represents peer data)
 */
public class DBTuple implements Serializable, Comparable<DBTuple>{
	private static final long serialVersionUID = -2487635685467654329L;

	private String peerName;
	private String ip;
	private String tcpPort;
	private String udpPort;
	private String phoneNum;
	private String emailAddress;
	private String lastConnectionTime;
	private String status;
	private String peerVersion;
	private String dbVersion;
	private String picSize;
	private String picVersion;
	private String peerCommitTime;
	private Set<String> peersWithNewPic;   // A set of the names of peers that have the picture of this peeer
	private List<String> incompletePicture; // A list of fragments of this picture while it is not completely accepted
	  
		/**
	 * size of the picture file
	 */
	
	public static String PIC_SIZE_STR = "PICTURE SIZE";
	private String rcvWin;
	private String pictureFile;
	
	public void setPeersWithNewPic(Set<String> peersWithNewPic){
		this.peersWithNewPic = peersWithNewPic;
	}
	
	public Set<String> getPeersWithNewPic(){
		return peersWithNewPic;
	}
	
	public void newSetOfPeersWithNewPic(){
		peersWithNewPic = new TreeSet<String>();
	}
	
	public void addToSetOfPeersWithNewPic(String peerWithPic){
		peersWithNewPic.add(peerWithPic);
	}
	
	public int getCountOfPeersWithNewPic(){
		return peersWithNewPic.size();
	}
	
	public boolean checkIfPeerInSet(String peerName){
		if(peersWithNewPic ==null){
			return false;
		}
		return peersWithNewPic.contains(peerName);
	}
	
	public List<String> getIncompletePicture(){
		return incompletePicture;
	}
	
	public void setIncompletePicture(List<String> incompetePicture){
		this.incompletePicture = incompetePicture;
	}
		
	public String getPeerName() {
		return peerName;
	}

	public void setPeerName(String peerName) {
		this.peerName = peerName;
		
	}

	public String getIp() {
		return ip;
	}

	public void setIp(String ip) {
		this.ip = ip;
	}

	public String getTcpPort() {
		return tcpPort;
	}

	public void setTcpPort(String tcpPort) {
		this.tcpPort = tcpPort;
	}

	public String getUdpPort() {
		return udpPort;
	}

	public void setUdpPort(String udpPort) {
		this.udpPort = udpPort;
	}

	public String getPhoneNum() {
		return phoneNum;
	}

	public void setPhoneNum(String phoneNum) {
		this.phoneNum = phoneNum;
	}

	public String getEmailAddress() {
		return emailAddress;
	}

	public void setEmailAddress(String emailAddress) {
		this.emailAddress = emailAddress;
	}

	public String getLastConnectionTime() {
		return lastConnectionTime;
	}

	public void setLastConnectionTime(String lastConnectionTime) {
		this.lastConnectionTime = lastConnectionTime;
	}

	public boolean getStatus() {
		return Integer.parseInt(status) == 1;
	}
	
	public String getStatusAsString() {
		return status;
	}

	public void setStatus(String status) {
		this.status = status;
	}

	public String getPeerVersion() {
		return peerVersion;
	}

	public void setPeerVersion(String peerVersion) {
		this.peerVersion = peerVersion;
	}

	public String getDbVersion() {
		return dbVersion;
	}

	public void setDbVersion(String dbVersion) {
		this.dbVersion = dbVersion;
	}

	public String getPicSize() {
		return picSize;
	}

	public void setPicSize(String picSize) {
		this.picSize = picSize;
	}

	public String getPicVersion() {
		return picVersion;
	}

	public void setPicVersion(String picVersion) {
		this.picVersion = picVersion;
	}

	public String getRcvWin() {
		return rcvWin;
	}

	public void setRcvWin(String rcvWin) {
		this.rcvWin = rcvWin;
	}
	
	public String getPictureFile() {
		return pictureFile;
	}

	public void setPictureFile(String pictureFile) {
		this.pictureFile = pictureFile;
	}

	//constructor
	public DBTuple(){
		incompletePicture = new ArrayList<String>();
		peersWithNewPic = new TreeSet<String>();
		
	}
	
	public static void main(String args){
		
	}
	
	@Override
	public String toString() {

		String sb = 
			"PEER NAME = " + peerName
		 +  "\nIP = " + ip
		 + 	"\nTCP = " + tcpPort
		 + 	"\nUDP = " + udpPort
		 + 	"\nPHONE = " + phoneNum
		 + 	"\nEMAIL = " + emailAddress
		 + 	"\nLAST CONNECTION TIME = " + lastConnectionTime
		 + 	"\nSTATUS = " + status
		 + 	"\nPEER VERSION = " + peerVersion
		 + 	"\nDB VERSION = " + dbVersion
		 + 	"\nPICTURE SIZE = " + picSize
		 +  "\nPICTURE NAME = " + pictureFile
		 + 	"\nPICTURE VERSION = " + picVersion
		 + 	"\nRECIEVE WINDOW = " + rcvWin;
		 //+ 	"\nBUFFER = " + buffer;

		return sb;
	}

	@Override
	//public int compareTo(Object arg0) {
	public int compareTo(DBTuple tuple) {
		// TODO Auto-generated method stub
		return this.peerName.compareTo(tuple.peerName);
	}
	
	public DBTuple clone(){
		DBTuple newTuple = new DBTuple();
		//newTuple.buffer = this.buffer;
		newTuple.dbVersion = this.dbVersion;
		newTuple.emailAddress = this.emailAddress;
		newTuple.ip = this.ip;
		newTuple.lastConnectionTime = this.lastConnectionTime;
		newTuple.peerName = this.peerName;
		newTuple.peerVersion = this.peerVersion;
		newTuple.phoneNum = this.phoneNum;
		newTuple.picSize = this.picSize;
		newTuple.pictureFile = this.pictureFile;
		newTuple.picVersion = this.picVersion;
		newTuple.rcvWin = this.rcvWin;
		newTuple.status = this.status;
		newTuple.tcpPort = this.tcpPort;
		newTuple.udpPort = this.udpPort;
		newTuple.peerCommitTime = this.peerCommitTime;
		newTuple.incompletePicture.addAll(this.incompletePicture);
		newTuple.peersWithNewPic.addAll(this.peersWithNewPic);
		
		return newTuple;
	}

	public void setPeerCommitTime(String peerCommitTime) {
		this.peerCommitTime = peerCommitTime;
	}

	public String getPeerCommitTime() {
		return peerCommitTime;
	}

}
